package com.stu.view;


import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Objects;

/**
 * @author Fairy
 */
public class MainFrame extends JFrame {

	private final JPanel contentPane;

	/**
	 * 布局
	 */
	public MainFrame() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/img/logo.png")));
		setTitle("学生管理系统主界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 972, 593);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu sysMenu = new JMenu("系统设置");
		sysMenu.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/系统设置.png"))));
		menuBar.add(sysMenu);
		JMenuItem exitItem = new JMenuItem("退出系统");
		exitItem.addActionListener(arg0 -> {
			if (JOptionPane.showConfirmDialog(MainFrame.this, "确定退出吗？") == JOptionPane.OK_OPTION) {
				System.exit(0);
			}
		});
		exitItem.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/退出.png"))));
		sysMenu.add(exitItem);
		
		JMenu stuMenu = new JMenu("学生管理");
		stuMenu.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/学生管理.png"))));
		menuBar.add(stuMenu);
		
		JMenuItem addStuItem = new JMenuItem("添加学生");
		addStuItem.addActionListener(this::studentAdd);
		addStuItem.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/添加.png"))));
		stuMenu.add(addStuItem);
		
		JMenuItem stuListItem = new JMenuItem("学生列表");
		stuListItem.addActionListener(this::studentsList);
		stuListItem.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/修改.png"))));
		stuMenu.add(stuListItem);
		
		JMenu helpMenu = new JMenu("帮助");
		helpMenu.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/帮助.png"))));
		menuBar.add(helpMenu);
		
		JMenuItem aboutItem = new JMenuItem("\u5173\u4e8e\u6211\u4eec");
		aboutItem.addActionListener(this::aboutus);
		aboutItem.setIcon(new ImageIcon(Objects.requireNonNull(MainFrame.class.getResource("/img/关于我们.png"))));
		helpMenu.add(aboutItem);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	}

	/**
	 * 学生管理
	 */
	protected void studentsList(ActionEvent e) {
		StudentsList studentsList = new StudentsList();
		studentsList.setVisible(true);
		contentPane.add(studentsList);
	}

	/**
	 * 添加学生
	 */
	protected void studentAdd(ActionEvent e) {
		AddStudent addStudent = new AddStudent();
		addStudent.setVisible(true);
		contentPane.add(addStudent);
	}

	/**
	 * 关于
	 */
	protected void aboutus(ActionEvent e) {
		String info = "\u8f6f\u4ef6\u0031\u0039\u002d\u0031\u73ed\u0020\u674e\u8d85\u51e1\u5f00\u53d1";
		JOptionPane.showMessageDialog(this, info);
	}
}
